﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    public partial class TWB03
    {
        public TWB03()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBDAT();
            this.DataContext = MV;
            InitializeComponent();
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwWB04 = dwMaster;

            if (dwMaster == null)
            {
                KEY = "";
                WAART = "";
            }
            else
            {
                WAART = dwMaster["WBART"].ToString() + dwMaster["WBVAR"].ToString();
                KEY = dwMaster["WBRWKRS"].ToString() + dwMaster["WBRWLFD"].ToString();
            }
        }
 
        public override void DeleteCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            PM["RWKRS"] = dwMaster["WBRWKRS"].ToString();
            PM["RWLFD"] = dwMaster["WBRWLFD"].ToString();
            TMV.DELWBDAT(PM);
            filter = "WBRWKRS='" + dwMaster["WBRWKRS"].ToString() + "' AND WBRWLFD='" + dwMaster["WBRWLFD"].ToString() + "'";
            vwMaster.View.MoveCurrentTo(-1);
            DataRow[] rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }

        public override void OnEKAUFChg(string ekauf)
        {
            TBMain.Clear();
            if (ekauf.Length < 12)
                return;
            dwData = TBMain.NewRow();
            dwData["WBMANDANT"] = app.Mandant;
            dwData["WBBESTKZ"] = ekauf.Substring(0, 1);
            dwData["WBBESTKRS"] = ekauf.Substring(1, 2);
            dwData["WBBESTLFD"] = ekauf.Substring(3, 6);
            dwData["WBBESTPOS"] = ekauf.Substring(9, 3);
            SResult = TMV.QueryWBDAT(dwData, 2);
            MergeData(SResult, false, true);
        }

        public override void LOADBYAUF()
        {
            TBMain.Clear();
            if (ORDERNUM.Length < 8)
            {
                return;
            }
            dwData = TBMain.NewRow();
            dwData["WBMANDANT"] = app.Mandant;
            dwData["WBRWKRS"] = ORDERNUM.Substring(0, 2);
            dwData["WBRWLFD"] = ORDERNUM.Substring(2, 6);
            SResult = ((VWBDAT)MV).QueryWBDAT(dwData, 1);
            MergeData(SResult, false);
            base.LOADBYAUF();
        }

        public override void OnISWB03Chg(bool value)
        {
            if (!value)
                return;
            string CART = dwEKP["PART"].ToString() + dwEKP["PVAR"].ToString();
            string CFARBE = dwEKP["PFARBE"].ToString();
            PM["WAKZ"] = dwEKP["EKPWAKZ"].ToString();
            PM["CART"] = CART;
            PM["CFARBE"] = CFARBE;
            PM["CBESCHICH"] = dwEKP["EKPBESCH"].ToString();
            PM["CDESS"] = dwEKP["EKPDESS"].ToString(); ;
            PM["CCOLORIT"] = dwEKP["EKPCOL"].ToString(); ;

            PM["WBRWKRS"] = "  ";
            PM["LO"] = LO;
            PM["LB"] = LB;
            PM["LP"] = LP;
            PM["ORDERNUM"] = dwEKP["EKPAUFKRS"].ToString() + dwEKP["EKPAUFLFD"].ToString() + dwEKP["EKPAUFPOS"].ToString() + "B";
            PM["ACTRLFD"] = ACTRLFD;
            PM["VKDELDATE"] = VKDELDATE;
            TMV.NEWWB04(PM);
            vwMaster.View.MoveCurrentToFirst();
            IsNew = false;
            IsWB03 = false;

        }
    }
}
